package com.vegetable.modules.controller.stock;

import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.stock.StockAlert;
import com.vegetable.modules.entity.stock.po.StockAlertQueryPO;
import com.vegetable.modules.entity.stock.po.StockAlertSavePO;
import com.vegetable.modules.service.stock.IStockAlertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 库存预警设置 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-09-09
 */
@Slf4j
@RestController
@RequestMapping("/stockAlert")
@AllArgsConstructor
@Api(value = "库存预警设置", tags = "库存预警设置")
public class StockAlertController extends AbstractController {

    private IStockAlertService stockAlertService;

    /**
     * 库存预警设置列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询库存预警设置列表", notes = "分页查询库存预警设置列表")
    public R list(@RequestBody StockAlertQueryPO po) {
        log.info("查询库存预警设置列表：{}", JSON.toJSONString(po));
        PageUtils page = stockAlertService.queryPage(po);
        return R.data(page);
    }

    /**
     * 库存预警设置详情
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询库存预警设置详情", notes = "查询库存预警设置详情")
    public R info(@PathVariable("id") Long id) {
        StockAlert stockAlert = stockAlertService.getById(id);
        return R.data(stockAlert);
    }

    /**
     * 新增库存预警设置
     */
    @SysLog(value = "新增库存预警设置")
    @PostMapping("/save")
    @ApiOperation(value = "新增库存预警设置", notes = "新增库存预警设置")
    public R save(@Valid @RequestBody StockAlertSavePO po) {
        log.info("新增库存预警设置：{}", JSON.toJSONString(po));
        boolean result = stockAlertService.saveOrUpdateAlert(po, getUserId());
        return R.status(result);
    }

    /**
     * 修改库存预警设置
     */
    @SysLog(value = "修改库存预警设置")
    @PostMapping("/update")
    @ApiOperation(value = "修改库存预警设置", notes = "修改库存预警设置")
    public R update(@Valid @RequestBody StockAlertSavePO po) {
        log.info("修改库存预警设置：{}", JSON.toJSONString(po));
        if (po.getId() == null) {
            return R.fail("ID不能为空");
        }
        boolean result = stockAlertService.saveOrUpdateAlert(po, getUserId());
        return R.status(result);
    }

    /**
     * 删除库存预警设置
     */
    @SysLog(value = "删除库存预警设置")
    @PostMapping("/delete")
    @ApiOperation(value = "删除库存预警设置", notes = "删除库存预警设置")
    public R delete(@RequestBody Long[] ids) {
        log.info("删除库存预警设置：{}", Arrays.toString(ids));
        stockAlertService.removeByIds(Arrays.asList(ids));
        return R.status(true);
    }

    /**
     * 批量启用/禁用预警设置
     */
    @SysLog(value = "批量启用/禁用预警设置")
    @PostMapping("/batchUpdateEnabled")
    @ApiOperation(value = "批量启用/禁用预警设置", notes = "批量启用/禁用预警设置")
    public R batchUpdateEnabled(@RequestParam List<Long> ids, @RequestParam Integer isEnabled) {
        log.info("批量更新预警设置状态，IDs：{}，状态：{}", ids, isEnabled);
        boolean result = stockAlertService.batchUpdateEnabled(ids, isEnabled);
        return R.status(result);
    }

    /**
     * 根据商品ID和规格ID查询预警设置
     */
    @GetMapping("/getByProduct")
    @ApiOperation(value = "根据商品ID和规格ID查询预警设置", notes = "根据商品ID和规格ID查询预警设置")
    public R getByProduct(@RequestParam Long commodityId, @RequestParam(required = false) Long specsId) {
        StockAlert alert = stockAlertService.getAlertByCommodityAndSpecs(commodityId, specsId);
        return R.data(alert);
    }

    /**
     * 手动执行库存预警检查
     */
    @SysLog(value = "手动执行库存预警检查")
    @PostMapping("/executeCheck")
    @ApiOperation(value = "手动执行库存预警检查", notes = "手动执行库存预警检查")
    public R executeCheck() {
        log.info("手动执行库存预警检查");
        try {
            stockAlertService.executeStockAlertCheck();
            return R.success("库存预警检查执行完成");
        } catch (Exception e) {
            log.error("执行库存预警检查失败", e);
            return R.fail("执行库存预警检查失败：" + e.getMessage());
        }
    }

    /**
     * 检查商品库存预警
     */
    @GetMapping("/checkAlert")
    @ApiOperation(value = "检查商品库存预警", notes = "检查商品库存预警")
    public R checkAlert(@RequestParam Long commodityId, 
                       @RequestParam(required = false) Long specsId,
                       @RequestParam Integer currentQuantity) {
        log.info("检查商品库存预警，商品ID：{}，规格ID：{}，当前数量：{}", commodityId, specsId, currentQuantity);
        try {
            boolean needAlert = stockAlertService.checkStockAlert(commodityId, specsId, currentQuantity);
            return R.data(needAlert);
        } catch (Exception e) {
            log.error("检查商品库存预警失败", e);
            return R.fail("检查商品库存预警失败：" + e.getMessage());
        }
    }
}